// Año: 2011
// Autor: Eduardo Sanchez

#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>
#include "client.h"


int connect_to_server(char server_name[50]){
    struct hostent *h;  /*info about the server*/
    struct sockaddr_in channel; /* holds IP address */
    int c,s = 1, ret;

    h = gethostbyname(server_name); /* look up host's IP address */    

    if (!h)
    {  fprintf(stderr,"gethostbyname failed.");
      return -1;
    }

    s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //create socket
    if (s<0) 
    { fprintf(stderr,"Socket failed.");
      return -1;
    } 

    memset(&channel, 0, sizeof(channel)); //inicializa la estructura a 0.
    channel.sin_family = AF_INET;
    memcpy(&channel.sin_addr.s_addr,h->h_addr,h->h_length);
    channel.sin_port = htons(SERVER_PORT);

    c = connect(s, (struct sockaddr *) &channel, sizeof(channel));
    if (c<0) 
    { fprintf(stderr,"Connect failed.");
      return -1;
    } 

    return s;
}




